package com.wisewin.pose.util;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;


public class OssUtil {
    private static String accessKeyId="LTAI4GARM7SL"+"R52v6ApSqKyY";
    private static String accessKeySecret="PpFJzMbfrS"+"vm2z5OJ8UZKSpwQTTZVT";
    private static String bucketName="xuedaozaixain";
    private static String endpoint = "oss-cn-beijing.aliyuncs.com";
    private static String url="https://xuedaozaixain.oss-cn-beijing.aliyuncs.com/";
    private static String path="/";
    public static String upload(MultipartFile file) throws IOException {
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        String fileName=file.getOriginalFilename();
        String uuid = UUID.randomUUID().toString().replaceAll("-","");
        Calendar calendar=Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH)+1;
        String newFileName="user/"+String.valueOf(year)+month+"/"+uuid+fileName;
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, newFileName,file.getInputStream());
        ossClient.putObject(putObjectRequest);
        ossClient.shutdown();
        return url+newFileName;
    }


    public static String uploadSecret(MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        String retrunName =  upload(file,fileName);
        File dest = new File(new File(path).getAbsolutePath()+ "/" + fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        file.transferTo(dest);
        return retrunName;
    }


    public static String upload(MultipartFile file, String fileName) throws IOException {
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, fileName,file.getInputStream());
        ossClient.putObject(putObjectRequest);
        ossClient.shutdown();
        return url+fileName;
    }


    public static String uploadCodePic(ByteArrayInputStream byteArrayInputStream,String dir) {
        String fileName = getFileName(UUID.randomUUID().toString()+".png", dir);
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, fileName,byteArrayInputStream);
        ossClient.putObject(putObjectRequest);
        ossClient.shutdown();
        return url + fileName;
    }

    private static String getFileName(String fileName, String dir) {
        String uploadDate = DateUtil.formatDate(new Date(), "yyyy-MM-dd");
        String uuid = dir + "/" + uploadDate + "/" + UUID.randomUUID().toString().replaceAll("-", "");
        return uuid + fileName;
    }
}
